package event

import (
	"api/app/tools/services"
	"api/app/wms/auto/v1/mc"
	"api/app/wms/conf"
	"api/app/wms/models"
	service "api/app/wms/services"
	"context"
	"gitee.com/dreamwood/ez"
)

func init() {
	ez.ListenJoin(mc.OutLogEventAfterCreate, func(v interface{}, ctx context.Context) {
		md, ok := v.(*models.OutLog)
		if ok {
			md.Type = new(models.IOType)
			md.Type.Id = md.TypeId
			md.Type.Read()
			//如果不填单号就自动创建
			if md.Sn == "" {
				//创建批次号
				sn := services.CreateSnByString("StoreOutSn", "[YY][MM][DD][DMAX[3]]", 1)
				md.Sn = sn[len("StoreOutSn:"):]
			}
			//如果不填批次号就自动创建
			if md.BatchCode == "" {
				//创建批次号
				sn := services.CreateSnByString("StoreOutBatch", "[YY][MM][DD][DMAX[3]]", 1)
				md.BatchCode = sn[len("StoreOutBatch:"):]
			}
			//如果不填堆号就自动创建
			if md.HeapCode == "" {
				//创建批次号
				sn := services.CreateSnByString("StoreOutHeap", "[YY][MM][DD][DMAX[3]]", 1)
				md.HeapCode = sn[len("StoreOutHeap:"):]
			}

			md.Save(false)
			service.CreateChangeLog(md.ItemId, conf.ChangeLogOut)
		}
	})
	ez.ListenJoin(mc.OutLogEventAfterUpdate, func(v interface{}, ctx context.Context) {
		md, ok := v.(*models.OutLog)
		if ok {
			md.Item = new(models.ItemSku)
			md.Item.Id = md.ItemId
			md.Item.Read()
			service.CreateChangeLog(md.ItemId, conf.ChangeLogOut)
		}
	})

}
